# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
贝壳-楼盘
https://sh.fang.ke.com/loupan/pg1/
"""



class BeiKe():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15'

        }


    def get_contents(self, cursor):
        postData = {

        }
        url = f"https://sh.fang.ke.com/loupan/pg{cursor}/?_t=1"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json['data']['list']:
            dict_data = dict()
            dict_data['title'] = d['title']  # 标题
            dict_data['address'] = d['address']  # 地址
            dict_data['city_name'] = d['city_name']  # 城市名
            dict_data['district_name'] = d['district_name']  # 城市
            dict_data['longitude'] = d['longitude']  # lon
            dict_data['latitude'] = d['latitude']  # lat
            dict_data['resblock_frame_area'] = d['resblock_frame_area']  # 建筑面积
            dict_data['frame_rooms_desc'] = d['frame_rooms_desc']  # 户型
            dict_data['decoration'] = d['decoration']  # 精装修
            dict_data['house_type'] = d['house_type']  # 住宅
            dict_data['show_price'] = d['show_price']  # 价格
            dict_data['show_price_unit'] = d['show_price_unit']  # 价格单位
            dict_data['total_price_start'] = d['total_price_start']  # 总价
            dict_data['total_price_start_unit'] = d['total_price_start_unit']  # 总价单位
            dict_data['open_date'] = d['open_date']  # 开盘时间
            dict_data['sale_status'] = d['sale_status']  # 销售状态 在售
            dict_data['tags'] = "|".join(d['tags'])  # 标签
            dict_data['developer_company'] = "|".join(d['developer_company'])  # 开发商
            dict_data['url'] = "https://sh.fang.ke.com" + d['url']  # 链接
            print(str(dict_data))


if __name__ == '__main__':
    beike = BeiKe()
    for i in range(1, 101):
        print(f"============第【{i}】页=============")
        beike.get_contents(i)